package com.toefl.speaking.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.toefl.speaking.R;
import com.toefl.speaking.activity.base.StepListActivity;
import com.toefl.speaking.business.QuestionUserBusiness;
import com.toefl.speaking.dao.bean.AudioInfo;

public class AudioActivity extends StepListActivity<AudioInfo> implements OnClickListener {
	public static final String TAG = "AudioActivity";

	public Button btn_delete, btn_cancel;

	private Button btn_titlebar_rigth;
	private LinearLayout edit_layout;
	private RadioButton radio_edit;

	private static Map<Integer, Integer> delete_radio_item = new HashMap<Integer, Integer>();

	private boolean isEdit = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.context = AudioActivity.this;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.audio);
		btn_delete = (Button) findViewById(R.id.btn_delete);
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		btn_titlebar_rigth = (Button) this.findViewById(R.id.audio_title_bar).findViewById(R.id.btn_titlebar_rigth);
		edit_layout = (LinearLayout) findViewById(R.id.edit_layout);
		ButtonsOnClickListener listener = new ButtonsOnClickListener();
		btn_titlebar_rigth.setOnClickListener(listener);
		load();
	}

	@Override
	public void onClick(View v) {
		int position = Integer.parseInt(v.getTag().toString());
		Intent intent = new Intent();
		intent.putExtra("question_id", 123);
		switch (v.getId()) {
		case R.id.radio_edit:

			// TODO 根据radioButton返回值的真假 做图片 map添加删除等处理
			// ((RadioButton)v).get
			delete_radio_item.put(position, position);
			break;
		}

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.audio_list_item, null);
			// 绑定事件
			convertView.findViewById(R.id.image_report).setOnClickListener(this);
			convertView.findViewById(R.id.image_submit).setOnClickListener(this);
			convertView.findViewById(R.id.text).setOnClickListener(this);
			if (isEdit) {
				convertView.findViewById(R.id.radio_edit).setVisibility(View.VISIBLE);
				convertView.findViewById(R.id.radio_edit).setOnClickListener(this);
			} else {
				convertView.findViewById(R.id.radio_edit).setVisibility(View.GONE);
			}
		}
		final AudioInfo audioInfo = getItem(position);
		convertView.setTag(position);
		if (audioInfo != null) {
			((TextView) convertView.findViewById(R.id.text)).setText((position + 1) + "."
					+ audioInfo.questionBean.question_title);
			((TextView) convertView.findViewById(R.id.text)).setTag(position);
			convertView.findViewById(R.id.image_report).setTag(position);
			convertView.findViewById(R.id.image_submit).setTag(position);
		}
		return convertView;
	}

	@Override
	protected ArrayList<AudioInfo> loadData() {
		return QuestionUserBusiness.getQuestionInfoByQuestionType("1", "222");
	}

	@Override
	protected void userOnPost() {
		super.userOnPost();
	}

	@Override
	public void userDoInBackground() {
		super.userDoInBackground();
	}

	private class ButtonsOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_titlebar_rigth:
				edit_layout.setVisibility(View.VISIBLE);
				isEdit = true;
				load();
				break;
			}
		}
	}

}